package net.knightwatch.kwtools.walkthrough.items;

import android.content.Context;
import android.graphics.Color;
import android.text.Editable;
import android.text.TextWatcher;
import android.util.Log;
import android.widget.EditText;
import android.widget.LinearLayout;

public class TextAnswer extends Answer {
	// Setup the name of the class type
	private static final String TAG = "TextAnswer";
	private String hintText;
	private int hintTextColor;
	private int minLength;
	private int maxLength;
	private EditText answer;
	private String answerValue;
	private int valueType;
	
	public TextAnswer(){
		this.setAnswerTypeId(TEXT_TYPE);
		this.setMinLength(0);
		this.setMaxLength(255);
		this.setHintTextColor(Color.GRAY);
		answerValue = "";
	}

	public String getHintText() {
		return hintText;
	}

	public void setHintText(String hintText) {
		this.hintText = hintText;
	}

	@Override
	public LinearLayout getAnswerLayout(Context context) {
		Log.v(TAG, "Generating a textview answer");
		// Create the text answer layout.
		LinearLayout answerLayout = new LinearLayout(context);
		answerLayout.setOrientation(LinearLayout.HORIZONTAL);
		// Setup the answer layout
		answer = new EditText(context);
		answer.setInputType(valueType);
		
		answer.setHint(this.getHintText());
		answerLayout.addView(answer);
		answer.addTextChangedListener(new TextWatcher(){
			public void afterTextChanged(Editable s)	{
				Log.d(TAG, "Answer text changed to " + s.toString());
				setAnswerValue(s.toString());
			}

			public void beforeTextChanged(CharSequence s, int start, int count,
					int after) {				
			}

			public void onTextChanged(CharSequence s, int start, int before,
					int count) {
				
			}
		});
		return answerLayout;
	}

	public int getMinLength() {
		return minLength;
	}

	public void setMinLength(int minLength) {
		this.minLength = minLength;
	}

	public int getMaxLength() {
		return maxLength;
	}

	public void setMaxLength(int maxLength) {
		this.maxLength = maxLength;
	}

	public int getHintTextColor() {
		return hintTextColor;
	}

	public void setHintTextColor(int gray) {
		this.hintTextColor = gray;
	}

	@Override
	public String toString() {
		// Return value as string. Easy to do with editbox value
		return answerValue;
	}

	public String getAnswerValue() {
		return answerValue;
	}

	public void setAnswerValue(String answerValue) {
		this.answerValue = answerValue;
	}

	@Override
	public void updateAnswerView() {
		// TODO remove?		
	}

	@Override
	public void setAnswerText(String answerText) {
		// TODO update the text view answer
		answer.setText(answerText);
		setAnswerValue(answerText);
	}

	public int getValueType() {
		return valueType;
	}

	public void setValueType(int valueType) {
		this.valueType = valueType;
	}
	
}
